local yise = fk.CreateSkill {

  name = "joy__yise",

  tags = { Skill.Compulsory, },

}



yise:addEffect(fk.AfterCardsMove, {
  name = "joy__yise",
  anim_type = "control",
  events = {fk.AfterCardsMove},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yise.name) then
      for _, move in ipairs(data) do
        if move.from==player and move.to and move.to ~= player.id and move.toArea == Card.PlayerHand then
          self.yise_to = move.to
          for _, info in ipairs(move.moveInfo) do
            self.yise_color = Fk:getCardById(info.cardId).color
              return true
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if self.yise_color == Card.Red then
      return player.room:askForSkillInvoke(player, yise.name, data, "#joy__yise-invoke1::"..self.yise_to)
    else
      return player.room:askForSkillInvoke(player, yise.name, data, "#joy__yise-invoke3::"..self.yise_to)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.yise_to)
    if self.yise_color == Card.Red then
      local choices = {"joy__yiseDrawCard","joy__yisecancel"}
      if to:isWounded() then
        table.insert(choices,"joy__yiseCover")
      end
      local choice = player.room:askForChoice(player,choices,yise.name)
      if choice == "joy__yiseDrawCard" and not player.dead then
        player:drawCards(1,yise.name)
      elseif choice == "joy__yiseCover" then
        room:recover({
          who = to,
          num = 1,
          recoverBy = player,
          skillName = yise.name
        })
      else
        return
      end
    elseif self.yise_color == Card.Black then
      room:addPlayerMark(to, "@joy__yise", 1)
    end
  end,
})

yise:addEffect(fk.DamageInflicted, {
  name = "#joy__yise_delay",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@joy__yise") > 0 and data.card and data.card.trueName == "slash"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + player:getMark("@joy__yise")
    player.room:setPlayerMark(player, "@joy__yise", 0)
  end,
})

return yise